/**
 * Filename: NewsCommentAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/4 17:17
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNNewsComment;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.TimeUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.LinkedList;

/**
 * 评论适配器
 */
public class NewsCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LinkedList<YNNewsComment> datas;
    private OnItemClickListener mListener;
    private OnItemLongClickListener mLongListener;
    private static final int EMPTY_VIEW = 10;

    public void setOnItemLongClickListener(OnItemLongClickListener mLongListener) {
        this.mLongListener = mLongListener;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public NewsCommentAdapter(Context context, LinkedList<YNNewsComment> datas) {
        this.context = context;
        this.datas = datas;
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_comment_root;
        SimpleDraweeView iv_news_comment_avatar;             // 评论人头像
        TextView tv_news_comment_name;               // 评论人名
        TextView tv_news_comment_time;               // 评论时间
        TextView tv_news_comment_content;            // 评论内容
        SimpleDraweeView iv_news_comment_pic;                // 评论动态图片

        public ViewHolder(View itemView) {
            super(itemView);
            rl_comment_root = (RelativeLayout) itemView
                    .findViewById(R.id.rl_comment_root);
            iv_news_comment_avatar = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_news_comment_avatar);
            tv_news_comment_name = (TextView) itemView
                    .findViewById(R.id.tv_news_comment_name);
            tv_news_comment_time = (TextView) itemView
                    .findViewById(R.id.tv_news_comment_time);
            tv_news_comment_content = (TextView) itemView
                    .findViewById(R.id.tv_news_comment_content);
            iv_news_comment_pic = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_news_comment_pic);
        }

        public void bindData(final YNNewsComment listItems, final int position) {
            if (listItems != null) {
                // 评论人头像
                String avatarPaths = YNUrlHelper.getAvatarUrl(context, listItems.getUpic());
                Uri avatarUri = Uri.parse(avatarPaths);
                ImageOptHelper.showFrescoAvatar(avatarUri, iv_news_comment_avatar, context);
                // 动态图片
                if (!StringUtils.isEmpty(listItems.getEfirstpic())) {
//                    String picPaths = YNUrlHelper.getAvatarUrl(context, listItems.getEfirstpic());
//                    Uri picUri = Uri.parse(picPaths);
//                    ImageOptHelper.showFrescoHome(picUri, iv_news_comment_pic, context);
                    String paths = YNUrlHelper.getStatusUrl(context, listItems.getEfirstpic());
                    String str1 = StringUtils.repaceImgUrl(paths);
                    ImageOptHelper.showFrescoStatus(Uri.parse(str1), iv_news_comment_pic, context);
                }
                tv_news_comment_name.setText(listItems.getUnickname());
                tv_news_comment_content.setText(listItems.getMcontent());
                //判断时间显示是否有问题
                if (TimeUtils.getGroupStatusTime(listItems.getMtime()).contains("HH")) {
                    //时间显示有问题出现字母用此方法显示时间
                    tv_news_comment_time.setText(TimeUtils.getUpLoadTime(listItems.getMtime()));
                } else {
                    tv_news_comment_time.setText(TimeUtils.getGroupStatusTime(listItems.getMtime()));
                }
                // 删除评论
                rl_comment_root.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (mLongListener != null) {
                            mLongListener.OnItemLongClick(v, position);
                        }
                        return true;
                    }
                });
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_empty_news, parent, false);
            v.getRootView().getLayoutParams().height = DisplayUtils.getScreenHeightPixels((Activity) context) - DisplayUtils.dp2px(context, 160);
            return new EmptyViewHolder(v);
        }

        final View convertView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.yn_item_news_comment, parent, false);
        // 查看评论详情
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.OnItemClick(v, ((YNNewsComment) convertView.getTag()).getMeid());
                }
            }
        });
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position) {
        if (rv_holder instanceof ViewHolder) {
            final ViewHolder holder = (ViewHolder) rv_holder;
            YNNewsComment listItems = getItem(position);
            holder.bindData(listItems, position);
            holder.itemView.setTag(listItems);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0) {
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    private YNNewsComment getItem(int position) {
        return datas.get(position);
    }

    @Override
    public int getItemCount() {
        return datas.size() > 0 ? datas.size() : 1;
    }

    public interface OnItemClickListener {
        void OnItemClick(View view, int mEid);
    }

    public interface OnItemLongClickListener {
        void OnItemLongClick(View view, int position);
    }
}
